package com.larissa.android.quiz;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.larissa.android.quiz.databinding.ActivityHelpBinding;
import com.larissa.android.quiz.question.Question;

import java.util.Date;
import java.util.Objects;


public class HelpActivity extends AppCompatActivity {

    private final String TAG = "HelpActivity";
    private ActivityHelpBinding binding;
    private HelpViewModel vm;

/*    private boolean showed;

    private String time;*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate() called");
        Objects.requireNonNull(getSupportActionBar()).setTitle("Help");
        binding = ActivityHelpBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
/*        if (savedInstanceState != null) {
            showed = savedInstanceState.getBoolean("showed");
            time = savedInstanceState.getString("time");
        }
        else {
            showed = false;
            time = "";
        }*/
        vm = new ViewModelProvider(this).get(HelpViewModel.class);

        int orientation = getResources().getConfiguration().orientation;
        //如果已经点击过了显示答案，那么将tvAnswer设置为可视，并且禁用“显示答案”按钮
        if (vm.isShowed()) {
            binding.tvAnswer.setVisibility(View.VISIBLE);
            if (orientation == Configuration.ORIENTATION_PORTRAIT)
                binding.showAnswer.setEnabled(false);
            else
                binding.showAnswerLand.setEnabled(false);
        }
        Question question = getIntent().getParcelableExtra("answer");
        //将文本设置为答案文本
        binding.tvAnswer.setText("The answer of " + question.getQuestionText() + " is " + question.getQuestionAnswer());
        //设置按钮点击事件
        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            binding.showAnswer.setOnClickListener(this::showAnswer);
        } else {
            binding.showAnswerLand.setOnClickListener(this::showAnswer);
        }
    }

    private void showAnswer(View view) {
        view.setEnabled(false);
        binding.tvAnswer.setVisibility(View.VISIBLE);
        vm.setShowed(true);
        vm.setTime(String.valueOf(new Date()));
    }


    @Override
    public void finish() {
        Intent result = new Intent();
        result.putExtra("showed", vm.isShowed());
        result.putExtra("time", vm.getTime());
        setResult(RESULT_OK, result);
        super.finish();
    }

/*    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean("showed", showed);
        outState.putString("time", time);
    }*/

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart() called");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart() called");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume() called");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause() called. finishing=" + isFinishing());
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop() called");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy() called. finishing=" + isFinishing());
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}